package com.ljx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.exception.GuliException;
import com.ljx.pojo.EduChapter;
import com.ljx.mapper.EduChapterMapper;
import com.ljx.pojo.EduVideo;
import com.ljx.pojo.chapter.ChapterVo;
import com.ljx.pojo.chapter.VideoVo;
import com.ljx.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoServiceImpl eduVideoService;
    //根据课程的Id查询出小节与章节的信息
    @Override
    public List<ChapterVo> getChapterById(String courseId) {
        //整体逻辑结构与课程分类的查询类似
        //查询所有的章节信息
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterQueryWrapper);

        //查询所有这个课程下面所有的小节信息
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(videoQueryWrapper);
        //封装章节信息
        ArrayList<ChapterVo> finAllChapter = new ArrayList<ChapterVo>();
        for (int i=0;i<eduChapters.size();i++){
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //封装小节信息
            ArrayList<VideoVo> videoVos = new ArrayList<>();
            //找在所有的二级分类里面 courseId是当前课程的id
            for (int m=0;m<eduVideos.size();m++){
                EduVideo eduVideo = eduVideos.get(m);
                VideoVo videoVo = new VideoVo();
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
            finAllChapter.add(chapterVo);
        }
        return finAllChapter;
    }
    //删除章节的方法
    @Override
    public Boolean deleteChapter(String chapterId) {
        //根据章节ID查询小节表 如果存在就不删除不存在就删除
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(eduVideoQueryWrapper);
        if (count>0){
            throw new GuliException(20001,"对不起该章节存在小节不能进行删除");
        }else{
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }
    }
    //删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
